package com.unidt.www.module.third.enterprise.controller.admin;

import com.unidt.www.framework.common.pojo.CommonResult;
import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.framework.common.util.object.BeanNullUtil;
import com.unidt.www.framework.operatelog.core.annotations.OperateLog;
import com.unidt.www.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceCreateReqVO;
import com.unidt.www.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceMyPageReqVO;
import com.unidt.www.module.bpm.controller.admin.task.vo.instance.BpmProcessInstancePageItemRespVO;
import com.unidt.www.module.bpm.framework.bpm.core.event.BpmCategory;
import com.unidt.www.module.bpm.service.task.BpmProcessInstanceService;
import com.unidt.www.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.unidt.www.module.system.service.organ.IOrganCodeService;
import com.unidt.www.module.system.service.organ.dto.OrganCodeDto;
import com.unidt.www.module.third.enterprise.dao.EnterpriseNoticeMapper;
import com.unidt.www.module.third.enterprise.domain.EnterpriseNoticeDO;
import com.unidt.www.module.third.enterprise.domain.SystemUserEnterpriseDO;
import com.unidt.www.module.third.enterprise.service.EnterpriseNoticeService;
import com.unidt.www.module.third.enterprise.service.SystemUserEnterpriseService;
import com.unidt.www.module.third.enterprise.vo.notice.EnterpriseNoticeCreatReqVO;
import com.unidt.www.module.third.enterprise.vo.notice.EnterpriseNoticeListReqVO;
import com.unidt.www.module.third.enterprise.vo.notice.EnterpriseNoticeUpdateReqVO;
import com.unidt.www.module.third.enterprise.vo.system.SystemUserEnterpriseQueryReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

import static com.unidt.www.framework.common.pojo.CommonResult.success;
import static com.unidt.www.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author ouyangoyys
 * @since 2022-11-24
 */
@Api(tags = "企业码-用户对接")
@RestController
@RequestMapping("/enterprise-do")
@Validated
public class EnterpriseNoticeController {

    @Resource
    private EnterpriseNoticeService enterpriseService;

    @Resource
    private IOrganCodeService organCodeService;

    @Resource
    private SystemUserEnterpriseService systemUserEnterpriseService;

    @Resource
    private BpmProcessInstanceService processInstanceService;



    @Resource
    private EnterpriseNoticeMapper enterpriseMapper;


    @PostMapping("/getEnterpriseTokenByUserInfo/{token}")
    @ApiOperation("企业码页面获取登录token")
    @OperateLog(enable = false)
    public CommonResult<AuthLoginRespVO> getEnterpriseTokenByUserInfo(@PathVariable(value = "token") String token) {

        return enterpriseService.getEnterpriseTokenByUserInfo(token);
    }

    @PostMapping("/getClientUserTokenByUserInfo/{token}")
    @ApiOperation("企业码的用户页面获取登录token")
    @OperateLog(enable = false)
    public CommonResult<AuthLoginRespVO> getClientUserTokenByUserInfo(@PathVariable(value = "token") String token) {

        return enterpriseService.getClientUserTokenByUserInfo(token);
    }



    @PostMapping("/getEnterpriseInfo")
    @ApiOperation("获取企业的详细信息")
    public CommonResult<SystemUserEnterpriseQueryReqVO> getEnterpriseInfo(@RequestParam("UserId") Long UserId) {


        SystemUserEnterpriseDO mSystemUserEnterpriseDO=systemUserEnterpriseService.selectByUserId(UserId);
        if(mSystemUserEnterpriseDO!=null) {
            SystemUserEnterpriseQueryReqVO mSystemUserEnterpriseQueryReqVO = new SystemUserEnterpriseQueryReqVO();
            BeanUtils.copyProperties(mSystemUserEnterpriseDO, mSystemUserEnterpriseQueryReqVO, BeanNullUtil.getNullPropertyNames(mSystemUserEnterpriseDO));
            OrganCodeDto mOrganCodeDto = organCodeService.getById(Long.valueOf(mSystemUserEnterpriseQueryReqVO.getCountyid()));
            mSystemUserEnterpriseQueryReqVO.setCountyname(mOrganCodeDto.getName());
            return success(mSystemUserEnterpriseQueryReqVO);

        }
        return success(null);
    }





    @PostMapping("create")
    @ApiOperation("创建企业通知基本信息")
    public CommonResult<Long> createEnterprise(@Valid @RequestBody EnterpriseNoticeCreatReqVO reqVO) {
        Long id = enterpriseService.createEnterprise(reqVO);
        return success(id);
    }

    @PutMapping("update")
    @ApiOperation("更新企业通知基本信息")
    public CommonResult<Boolean> updateEnterprise(@Valid @RequestBody EnterpriseNoticeUpdateReqVO reqVO) {
        enterpriseService.updateEnterprise(reqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @ApiOperation("删除企业通知基本信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteEnterprise(@RequestParam("id") Long id) {
        enterpriseService.deleteEnterprise(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获取企业通知信息")
    public CommonResult<EnterpriseNoticeDO> getEnterpriseNotice(@RequestParam("id") Long id) {
        return success(enterpriseMapper.selectById(id));
    }


    @GetMapping("/list")
    @ApiOperation("获取企业[通知]信息列表")
    public CommonResult<PageResult<EnterpriseNoticeDO>> listDepts(EnterpriseNoticeListReqVO reqVO) {
        PageResult<EnterpriseNoticeDO> list = enterpriseService.getSimpleEnterprise(reqVO);
        return success(list);
    }

    @GetMapping("/my-declare")
    @ApiOperation(value = "获得我的申报分页列表", notes = "我的申报")
    public CommonResult<PageResult<BpmProcessInstancePageItemRespVO>> getMyProcessInstancePage(
            @Valid BpmProcessInstanceMyPageReqVO pageReqVO) {

        PageResult<BpmProcessInstancePageItemRespVO> mPageResult=processInstanceService.getMyDeclareInstancePage(getLoginUserId(), pageReqVO);

        List<BpmProcessInstancePageItemRespVO> mlist=mPageResult.getList().stream()
                   .filter(p-> p.getCategory().equals(BpmCategory.CATEGORY_DECLARE))
                   .collect(Collectors.toList());

        if( mlist!=null) {
            mPageResult.setList(mlist);
            mPageResult.setTotal(Long.valueOf(mlist.size()));
        }
        return success(mPageResult);
    }


    @PostMapping("/create-declare")
    @ApiOperation("开始申报-创建申报实例")
    public CommonResult<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
        return success(processInstanceService.createDeclareProcessInstance(getLoginUserId(), createReqVO));
    }


}
